﻿$(function () {
    $(":file").filestyle();

    $(".selectpicker").selectpicker();

    load();
    function load() {


        $.ajax({
            url: "/Home/MethodList",
            type: "GET",
            data: { "tableName":"tb_Method"},
            dataType: "JSON",
            success: function (data) {
            
                var t1 = $("#t1 tbody");
                t1.children().remove();
                t1.hide();
                //console.info(data);
                //JSON.parse(data)
                $.each(data, function (index, Element) {

                    var id = Element.id;
                    var title = Element.title;
                    var methodContent = Element.methodContent;
                    var tr = $('<tr><td data-id="' + id + '">' + id + '</td><td data-title="' + title + '">' + l2s(title) + '</td><td data-methodContent="' + methodContent + '">' + l2s(methodContent) + '</td><td data-id="' + id + '"><button type="button" class="btn btn-sm btn-primary btn-edit"><i class="fa fa-pencil-square-o"></i> 编辑</button><button type="button" class="btn btn-sm btn-danger btn-delete"><i class="fa fa-times"></i> 删除</button></td></tr>');
                    t1.append(tr);

                })
                t1.show(500);
            }
        });
    }
    var table = $("#t1 tbody");
    table.on("click", "button", function () {
        var id = $(this).parent().data("id");
        if ($(this).hasClass("btn-edit")) {
            //edit           
            var tr = $(this).parent().parent();
            $("#Featured_Title").val(tr.children(":eq(1)").data("title"));
            $("#field_MethodContent").val(tr.children(":eq(2)").data("methodcontent"));
            
            $("#createform").data("cmd", "up").data("id", id);
            $("#createmodal").modal("show");
        }
        else {
            //delete
            var deletemodal = $("#deletemodal");
            deletemodal.modal("show").on("click", "button", function () {

                $.ajax({
                    url: "api/Default",
                    type: "DELETE",
                    data: {"id":id, "tableName": "tb_Method" },
                    success: function (data) {
                        deletemodal.off("click", "button").modal("hide");
                        if (data == 1) {
                            load();
                        } else {
                            alert(data);
                        }

                    }
                })
            })
        }
    });

    function l2s(str) {
        if (str == "") {
            return "";
        }
        if (str.length <= 15) {
            return str;
        }
        return str.substring(-1, 15) + "...";
    }

    $("#create").click(function () {
        $("#createform").data("cmd", "new").data("id", -1)[0].reset();
        $("#Featured_Image + div").children().val("");
        $("#Featured_Type").val("0");
        $(".selectpicker").selectpicker("val", "0");
        $("#createmodal").modal("show");
    })

    $(document).on("click", "#ok", function () {		
        var cmd = $("#createform").data("cmd");
        var id = $("#createform").data("id");
        var url = "api/Default";
        var data = { "id":id,"title": $("#Featured_Title").val(), "methodContent": $("#field_MethodContent").val(), "tableName": "tb_Method" };
        var type = "POST";

        if (cmd == "up") {
            //url += "/" + id;
            type = "PUT";
        }
        $.ajax({
            url: url,
            type: type,
            data: data,
            success: function (data) {
                $("#createmodal").modal("hide");
                load();
            }
        })
    })

    $("#imageform").ajaxForm({
        dataType: "json",
        beforeSubmit: function () {
            $("#imageprogress").css("width", "10%").text("");
        },
        uploadProgress: function (event, position, total, percentComplete) {
            $("#imageprogress").css("width", percentComplete + "%").text("上传进度:" + percentComplete + "%");
        },
        success: function (data) {
            if (data.ErrCode == 0) {
                $("#imageprogress").css("width", "100%").text("上传完成!");
                //$("#Featured_Image").val(tr.children(":eq(4)").data("image"));
                $("#Featured_Image + div").children().val(data.Result);
            }
            else {
                alert(data.ErrString);
                $("#imageprogress").css("width", "10%").text("");
            }
        }
    });


    $("#Featured_Image").change(function () {
        if ($(this).val() != "") {
            $("#imageform").submit();
        }
        else {
            $("#imageprogress").css("width", "10%").text("");
        }
    })

    function type2name(type) {
        var typeArray = new Array("无", "项目聚焦", "重点项目", "BHI视点", "投资动态", "投资政策", "行业动态", "经济要闻", "信息简报", "一带一路专题", "PPP", "PPP热点", "一带一路");
        return typeArray[parseInt(type)];
    }
})


